package manager_service.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import manager_mapper.mapper.ManagerMapper;
import manager_mapper.mapper.ResourceMapper;
import manager_service.service.BackResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import test_common.constant.Constant;
import test_common.entity.*;
import test_common.utils.ContentIsNull;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangzheng
 * @create 2021/1/8
 */
@Service
@Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
public class BackResourceServiceImpl implements BackResourceService {

    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private ResourceMapper resourceMapper;
    @Override
    public void addResource(Resource resource) {
        resourceMapper.insertSelective(resource);

    }

    @Override
    public List<ResourceVo> showResources(String userId,String chapterId,String courseId) {
        ResourceExample example=new ResourceExample();
                example.or()
                .andChapterIdEqualTo(Integer.parseInt(chapterId))
                .andCourseIdEqualTo(Integer.parseInt(courseId));
//                .andUserIdEqualTo(Integer.parseInt(userId));
        List<Resource> resources=resourceMapper.selectByExample(example);
//        Manager manager=managerMapper.selectByPrimaryKey(Integer.parseInt(userId));
        List<ResourceVo> resourceVos=new ArrayList<ResourceVo>();
        for (int i=0;i<resources.size();i++) {
                    ResourceVo resourceVo=new ResourceVo();
                    resourceVo.setChapterId(resources.get(i).getChapterId());
                    resourceVo.setClickCount(resources.get(i).getClickCount());
                    resourceVo.setCostNumber(resources.get(i).getCostNumber());
                    resourceVo.setCostType(resources.get(i).getCostType());
                    resourceVo.setCoverImageUrl(resources.get(i).getCoverImageUrl());
                    resourceVo.setCreateDate(resources.get(i).getCreateDate());
                    resourceVo.setFileSize(resources.get(i).getFileSize());
                    resourceVo.setFileType(resources.get(i).getFileType());
                    resourceVo.setOriginalName(resources.get(i).getOriginalName());
                    resourceVo.setId(resources.get(i).getId());
                    resourceVo.setPath(resources.get(i).getPath());
                    resourceVo.setTitle(resources.get(i).getTitle());
                    resourceVo.setStatus(resources.get(i).getStatus());
            Manager manager=managerMapper.selectByPrimaryKey(resources.get(i).getUserId());
                    resourceVo.setUserName(manager.getName());
                    resourceVo.setTotalTime("20");
                    resourceVo.setCoverImageUrl("none");
                    resourceVos.add(resourceVo);
                }
        return resourceVos.isEmpty()?null:resourceVos;

        }

    @Override
    public void modifyStatus(Integer id) {
        Resource resource=resourceMapper.selectByPrimaryKey(id);
        if (resource.getStatus()== Constant.Resource_ENABLE){
            resource.setStatus(Constant.Resource_DISABLE);
        }
        else if (resource.getStatus()==Constant.Resource_DISABLE){
            resource.setStatus(Constant.Resource_ENABLE);
        }
        resourceMapper.updateByPrimaryKey(resource);
    }

    @Override
    public List<ResourceVo> searchResources(String title, String userId, String status, String fileType, Date dateStart, Date dateEnd, String courseId, String chapterId) {

        List<Resource> resources = resourceMapper.searchResources(title, userId, status, fileType, dateStart, dateEnd, courseId, chapterId);

        List<ResourceVo> resourceVos = new ArrayList<ResourceVo>();
        for (int i = 0; i < resources.size(); i++) {
            ResourceVo resourceVo = new ResourceVo();
            resourceVo.setChapterId(resources.get(i).getChapterId());
            resourceVo.setClickCount(resources.get(i).getClickCount());
            resourceVo.setCostNumber(resources.get(i).getCostNumber());
            resourceVo.setCostType(resources.get(i).getCostType());
            resourceVo.setCoverImageUrl(resources.get(i).getCoverImageUrl());
            resourceVo.setCreateDate(resources.get(i).getCreateDate());
            resourceVo.setFileSize(resources.get(i).getFileSize());
            resourceVo.setFileType(resources.get(i).getFileType());
            resourceVo.setOriginalName(resources.get(i).getOriginalName());
            resourceVo.setId(resources.get(i).getId());
            resourceVo.setPath(resources.get(i).getPath());
            resourceVo.setTitle(resources.get(i).getTitle());
            resourceVo.setStatus(resources.get(i).getStatus());
            Manager manager = managerMapper.selectByPrimaryKey(resources.get(i).getUserId());
            resourceVo.setUserName(manager.getName());
            resourceVos.add(resourceVo);
        }

        return resourceVos;
    }

    @Override
    public Resource selectResource(String id) {
        Resource resource=resourceMapper.selectByPrimaryKey(Integer.parseInt(id));
        return resource;
    }

    @Override
    public void modifyResource(Resource resource) {
        resourceMapper.updateByPrimaryKeySelective(resource);
    }
}
